class Compiler {
  constructor(vm) {
    this.el = vm.$el
    this.vm = vm
    this.compile(this.el)
  }
  
  // 编译模板，处理文本节点和元素节点
  compile(el) {
    let childNodes = el.childNodes
    Array.from(childNodes).forEach(node => {
      // 处理文本节点
      if (this.isTextNode(node)) {
        this.compileText(node)
      } else if (this.isElementNode(node)) {
        // 处理元素节点
        this.compileElement(node)
      }

      if (node.childNodes && node.childNodes.length) {
        this.compile(node)
      }
    })
  }

  // 编译元素节点，处理指令
  compileElement(node) {
    Array.from(node.attributes).forEach(attr => {
      let attrName = attr.name
      if (this.isDirective(attrName)) {
        attrName = attrName.includes('v-on') ? attrName.substring(2, 3) : attrName.substring(2)
        const event = attrName.includes('v-on') && attrName.substring(5)
        const key = attr.value
        this.update(node, key, attrName, event)
      }
    })
  }

  update(node, key, attrName, event) {
    const updateFn = this[attrName + 'Updater']
    updateFn && updateFn(node, this.vm[key], event)
  }

  // 处理v-text指令
  textUpdater(node, value) {
    node.textContent = value
  }

  // 处理v-model指令
  modelUpdater(node, value) {
    node.value = value
  }

  // 处理v-html指令
  htmlUpdater(node, value) {
    node.innerHTML = value
  }

  // 处理v-on指令
  onUpdater(node, listener, event) {
    node.addEventListener(event, listener, false)
  }

  // 编译文本节点，处理插值表达式
  compileText(node) {
    const reg = /\{\{(.+?)\}\}/
    const value = node.textContent
    if (reg.test(value)) {
      const key = RegExp.$1.trim()
      node.textContent = value.replace(reg, this.vm[key])
    }
  }

  // 判断元素属性是否是指令
  isDirective(attrName) {
    return attrName.startsWith('v-')
  }

  // 判断节点是否是文本节点
  isTextNode(node) {
    return node.nodeType === 3
  }

  // 判断节点是否是元素节点
  isElementNode(node) {
    return node.nodeType === 1
  }
}